package cn.uin.domain.shared.adapter.repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName SimpleSort
 * @Description:简单排序实现类
 * @Author: Administrator
 * @CreateDate: 2025/11/14 17:24
 * @Version: 1.0
 */
public class SimpleISort implements ISort {

	private final List<Order> orders;

	/**
	 * 构造函数
	 * @param orders 排序规则列表
	 */
	public SimpleISort(List<Order> orders) {
		this.orders = orders != null ? Collections.unmodifiableList(new ArrayList<>(orders)) : Collections.emptyList();
	}

	@Override
	public List<Order> getOrders() {
		return Collections.unmodifiableList(orders);
	}

	@Override
	public boolean isSorted() {
		return !orders.isEmpty();
	}

	@Override
	public boolean isUnsorted() {
		return orders.isEmpty();
	}

	@Override
	public ISort and(ISort ISort) {
		if (ISort == null || ISort.isUnsorted()) {
			return this;
		}

		// 合并排序规则
		List<Order> newOrders = new java.util.ArrayList<>(this.orders);
		newOrders.addAll(ISort.getOrders());
		return new SimpleISort(newOrders);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (!(obj instanceof SimpleISort)) return false;
		SimpleISort that = (SimpleISort) obj;
		return orders.equals(that.orders);
	}

	@Override
	public int hashCode() {
		return orders.hashCode();
	}

	@Override
	public String toString() {
		if (orders.isEmpty()) {
			return "UnSorted";
		}
		return "Sort: " + orders;
	}
}
